package com.skillworld.webapp.model.gamemap;

import org.springframework.stereotype.Repository;

import com.skillworld.webapp.model.gamemap.GameMap;
import com.skillworld.webapp.model.gamemap.GameMapDao;
import com.skillworld.webapp.model.util.dao.GenericDaoHibernate;

import es.udc.pojo.modelutil.exceptions.InstanceNotFoundException;

@Repository("GameMapDaoDao")
public class GameMapDaoHibernate 
    extends GenericDaoHibernate<GameMap,Long> 
    implements GameMapDao 
{
    public GameMap findByName(String name) throws InstanceNotFoundException
    {
        GameMap gameMap =(GameMap) 
            getSession().createQuery("SELECT map FROM GameMap map " +
                                     "WHERE map.name = :name")
                               .setParameter("name", name)
                               .uniqueResult();
        
        if (gameMap == null)
        {
            throw new InstanceNotFoundException(name,GameMap.class.getName());
        }
        
        return gameMap;
    }
    
    public GameMap getMainMap()
    {
        try
        {
            return findByName(GameMap.MAIN_MAP_NAME);
        }
        catch(InstanceNotFoundException e)
        {
            // Change to something like "ImpossibleHappensException"
            throw new RuntimeException(e);
        }
    }
}
